package com.loser.utils;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Objects;

/**
 * 断言工具
 *
 * @author alan
 * @date 2022/5/30 19:09
 */
public class AssertUtil {

    private static Class<? extends RuntimeException> EClass;

    public static void setEClass(Class<? extends RuntimeException> clazz) {
        EClass = clazz;
    }

    public static void notNull(Object obj, String msg) {
        if (Objects.isNull(obj)) {
            throwBiz(msg);
            throw new BusinessException(msg);
        }
    }

    private static void throwBiz(String msg) {

        if (Objects.nonNull(EClass)) {
            RuntimeException exception = null;
            try {
                Constructor<? extends RuntimeException> constructor = EClass.getConstructor();
                exception = constructor.newInstance();
                Field field = EClass.getDeclaredField("message");
                field.setAccessible(true);
                field.set(exception, msg);
            } catch (Exception ignore) {
            }
            if (Objects.nonNull(exception)) {
                throw exception;
            }
            try {
                exception = EClass.getConstructor(String.class).newInstance(msg);
            } catch (Exception ignore) {
            }
            if (Objects.nonNull(exception)) {
                throw exception;
            }
        }

    }

    public static void notTrue(boolean isNotTrue, String msg) {
        if (!isNotTrue) {
            throwBiz(msg);
            throw new BusinessException(msg);
        }
    }

    public static void isTrue(boolean isTrue, String msg) {
        if (isTrue) {
            throwBiz(msg);
            throw new BusinessException(msg);
        }
    }

    public static void notEmpty(String str, String msg) {
        if (StringUtils.isEmpty(str)) {
            throwBiz(msg);
            throw new BusinessException(msg);
        }
    }

    public static void notEmpty(Collection<?> list, String msg) {
        if (CollectionUtils.isEmpty(list)) {
            throwBiz(msg);
            throw new BusinessException(msg);
        }
    }

    public static void lteZero(int obj, String msg) {
        if (obj <= 0) {
            throwBiz(msg);
            throw new BusinessException(msg);
        }
    }

    public static void notNull(Object obj, RuntimeException e) {
        if (Objects.isNull(obj)) {
            throw e;
        }
    }

    public static void notTrue(boolean isNotTrue, RuntimeException e) {
        if (!isNotTrue) {
            throw e;
        }
    }

    public static void isTrue(boolean isTrue, RuntimeException e) {
        if (isTrue) {
            throw e;
        }
    }

    public static void notEmpty(String str, RuntimeException e) {
        if (StringUtils.isEmpty(str)) {
            throw e;
        }
    }

    public static void notEmpty(Collection<?> list, RuntimeException e) {
        if (CollectionUtils.isEmpty(list)) {
            throw e;
        }
    }

    public static void lteZero(int obj, RuntimeException e) {
        if (obj <= 0) {
            throw e;
        }
    }

}
